package com.next.aappublicapp.util;

import java.io.File;
import java.io.IOException;

import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;

public class DiskUtil {

	private static final String DISK_CACHE_SUBDIR = "thumbnails";
	private static final Object mDiskCacheLock = new Object();
	private static DiskLruCache mDiskLruCache;
	private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10;
	private static boolean mDiskCacheStarting;

	public static void initializeDiskCache(Context context){
	    // Initialize disk cache on background thread
	    File cacheDir = getDiskCacheDir(context, DISK_CACHE_SUBDIR);
	    new InitDiskCacheTask().execute(cacheDir);
	    
	}

	static class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
	    @Override
	    protected Void doInBackground(File... params) {
	        synchronized (mDiskCacheLock) {
	            File cacheDir = params[0];
	            try {
					mDiskLruCache = DiskLruCache.open(cacheDir,1,100, DISK_CACHE_SIZE);
		            mDiskCacheStarting = false; // Finished initialization
		            mDiskCacheLock.notifyAll(); // Wake any waiting threads
				} catch (IOException e) {
					e.printStackTrace();
				}
	        }
	        return null;
	    }
	}
	
	public static void addBitmapToCache(String key, Bitmap bitmap) {
	    // Add to memory cache as before
		/*
	    if (getBitmapFromMemCache(key) == null) {
	        mMemoryCache.put(key, bitmap);
	    }*/

	    // Also add to disk cache
		/*
	    synchronized (mDiskCacheLock) {
	        if (mDiskLruCache != null && mDiskLruCache.get(key) == null) {
	        	//mDiskLruCache.edit(key).
	            //mDiskLruCache.(key, bitmap);
	        	//ImageLoader.getInstance().displayImage(uri, imageView, options)
	        }
	    }
	    */
	}

	public static Bitmap getBitmapFromDiskCache(String key) {
	    synchronized (mDiskCacheLock) {
	        // Wait while disk cache is started from background thread
	        while (mDiskCacheStarting) {
	            try {
	                mDiskCacheLock.wait();
	            } catch (InterruptedException e) {}
	        }
	        if (mDiskLruCache != null) {
	            //return mDiskLruCache.get(key);
	        }
	    }
	    return null;
	}
	// Creates a unique subdirectory of the designated app cache directory. Tries to use external
	// but if not mounted, falls back on internal storage.
	public static File getDiskCacheDir(Context context, String uniqueName) {
	    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
	    // otherwise use internal cache dir
	    final String cachePath =
	            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
	                    !Environment.isExternalStorageRemovable() ? context.getExternalCacheDir().getPath() :
	                            context.getCacheDir().getPath();

	    return new File(cachePath + File.separator + uniqueName);
	}
}
